package net.oschina.aibinxiao.security.sha;

import com.sun.org.apache.xpath.internal.SourceTree;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA224Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

/**
 * @Author: AiBinXiao
 * @Description: 消息摘要算法SHA
 * @Date: 2017/10/13 11:19
 */

public class MySHA {
    private static String src = "imooc security sha";

    public static void main(String[] args) {
        jdkSHA1();
        bcSHA1();
        bcSHA224();
        bcSHA224_2();
        ccSHA1();
    }

    // JDK实现的SHA1消息摘要算法
    public static void jdkSHA1() {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(src.getBytes());
            System.out.println("JDK SHA1 : " + Hex.encodeHexString(md.digest()));
            // JDK SHA1 : 8bb95fe138f7e9d993dd90bae339a45e56b72eb7
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    // Bouncy Castle实现的SHA1消息摘要算法
    public static void bcSHA1() {
        Digest digest = new SHA1Digest();
        digest.update(src.getBytes(), 0, src.getBytes().length);
        byte[] sha1Bytes = new byte[digest.getDigestSize()];
        digest.doFinal(sha1Bytes, 0);
        System.out.println("Bouncy Castle SHA1 : " + org.bouncycastle.util.encoders.Hex.toHexString(sha1Bytes));
        // Bouncy Castle : 8bb95fe138f7e9d993dd90bae339a45e56b72eb7
    }

    // Bouncy Castle实现的SHA224消息摘要算法
    public static void bcSHA224(){
        Digest digest = new SHA224Digest();
        digest.update(src.getBytes(), 0, src.getBytes().length);
        byte[] sha224Bytes = new byte[digest.getDigestSize()];
        digest.doFinal(sha224Bytes, 0);
        System.out.println("Bouncy Castle SHA224 : " + org.bouncycastle.util.encoders.Hex.toHexString(sha224Bytes));
        // Bouncy Castle : 0b71cc6f590e1d026c922a99081a14e2c58818f4047dae6db3cf3d67
    }

    // Bouncy Castle实现的SHA224消息摘要算法 方法二
    public static void bcSHA224_2(){
        Security.addProvider(new BouncyCastleProvider());
        try {
            MessageDigest md = MessageDigest.getInstance("SHA224");
            byte[] sha224Bytes = md.digest(src.getBytes());
            System.out.println("Bouncy Castle SHA224 : " + Hex.encodeHexString(sha224Bytes));
            // Bouncy Castle SHA224 : 0b71cc6f590e1d026c922a99081a14e2c58818f4047dae6db3cf3d67
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    // Commons Codec 实现的SHA1消息摘要算法
    public static void ccSHA1() {
        System.out.println("Commons Codec SHA1 : " + DigestUtils.sha1Hex(src.getBytes()));
        System.out.println("Commons Codec SHA1 : " + DigestUtils.sha1Hex(src));
        // Commons Codec SHA1 : 8bb95fe138f7e9d993dd90bae339a45e56b72eb7
    }

}
